/**
 * Copyright 1998-2006, CHISEL Group, University of Victoria, Victoria, BC, Canada.
 * All rights reserved.
 */
package ca.uvic.cs.cogz.mapping.ui.tree;

import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.tree.TreePath;

import edu.stanford.smi.protege.util.LazyTreeNode;

/**
 * This class is an extension of the BasicTreeUI and is used by the source and target ontology
 * trees. The class overloads several functions from BasicTreeUI in order to handle the zoom
 * effects.
 * 
 * @author Sean Falconer
 * @date 22-Feb-07
 */
public class ZoomableTreeUI extends BasicTreeUI {
	private ZoomManager zoomManager;

	public ZoomableTreeUI() {
		super();
	}

	public ZoomableTreeUI(ZoomManager zoomManager) {
		super();

		this.zoomManager = zoomManager;
	}

	public static ComponentUI createUI(JComponent x) {
		return new ZoomableTreeUI();
	}

	public void paint(Graphics g, JComponent c) {
		if (zoomManager.getTree() != null) {
			/*if(zoomManager.getTree().getRowHeight() <= 0) {
				zoomManager.getTree().setRowHeight(1);
			}
			zoomManager.getTree().setRowHeight(0);*/

			super.paint(g, c);
		}
	}

	public void updateSize() {
		// @tag note : needed to expose the protected method so that scrollbars will resize
		super.updateSize();
	}

//	protected void paintRow(Graphics g, Rectangle clipBounds,
//		    Insets insets, Rectangle bounds, TreePath path,
//		    int row, boolean isExpanded,
//		    boolean hasBeenExpanded, boolean isLeaf) {
//
//		//System.out.println(row);
//		
//		// @tag note : this call is necessary in order to force the MapRenderer's size calculation to be used 
//		//treeState.setNodeDimensions(new NodeDimensionsHandler());
//
//		super.paintRow(g, clipBounds, insets, bounds, path, row, isExpanded, hasBeenExpanded, isLeaf);
//	}

	// @tag note : needed in order to resize expand and collapse icons based on zooming
	@Override
	protected void paintExpandControl(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) {
		Object value = path.getLastPathComponent();

		// Draw icons if not a leaf and either hasn't been loaded,
		// or the model child count is > 0.
		if (!isLeaf && (!hasBeenExpanded || treeModel.getChildCount(value) > 0)) {
			int middleXOfKnob = bounds.x - (getRightChildIndent() - 1);
			int middleYOfKnob = bounds.y + (bounds.height / 2);

			if (isExpanded) {
				Icon expandedIcon = getExpandedIcon();
				if (value instanceof LazyTreeNode) {
					expandedIcon = zoomManager.getScaledIcon(ZoomManager.ICON_TREE_EXPANDED, value);
				}
				if (expandedIcon != null) {
					drawCentered(tree, g, expandedIcon, middleXOfKnob, middleYOfKnob);
				}
			} else {
				Icon collapsedIcon = getCollapsedIcon();
				if (value instanceof LazyTreeNode) {
					collapsedIcon = zoomManager.getScaledIcon(ZoomManager.ICON_TREE_COLLAPSED, value);
				}
				if (collapsedIcon != null) {
					drawCentered(tree, g, collapsedIcon, middleXOfKnob, middleYOfKnob);
				}
			}
		}
	}
}
